package com.app.util.common;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;



public class ResponseUtil {

	private final static Logger log = Logger.getLogger(ResponseUtil.class);
	
	
	public static void write(String str, HttpServletResponse response) {
		log.info("response结果："+str);
		try {
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(str);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 将对象转换成JSON字符串，并响应回前台
	 * 
	 * @param object
	 * @throws IOException
	 */
	public static void writeJson(Object object, HttpServletResponse response) {
		try {
			//String json = JSON.toJSONString(object);
			String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd",SerializerFeature.DisableCircularReferenceDetect);
//			System.out.println(json);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 将对象转换成JSON字符串，并响应回前台
	 * 
	 * @param object
	 * @throws IOException
	 */
	public static void writeJson(Object object, HttpServletResponse response,String type) {
		try {
			String json = JSON.toJSONStringWithDateFormat(object, type,SerializerFeature.DisableCircularReferenceDetect);
			
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e){
			e.printStackTrace();
		}
	}
	
	public static void writeJsonString(HttpServletResponse response,String jsonstring){
		try{
			
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(jsonstring);
			response.getWriter().flush();
			response.getWriter().close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
